package com.warrior.service;

import com.warrior.entity.WxCache;
import com.warrior.respository.IWxCacheRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Shawn on 2017/4/6.
 */
@Service
public class WxCacheService {

    @Autowired
    private IWxCacheRepository wxCacheRepository;


    @Transactional
    public WxCache saveWxCache(WxCache wxCache){
        this.wxCacheRepository.save(wxCache);
        return  wxCache;
    }

    @Transactional
    public WxCache findCache(){
        List<WxCache> wxCaches = this.wxCacheRepository.findAll();
        if (null!=wxCaches){
            //取第一条
            return wxCaches.get(0);
        }else{
            return null;
        }

    }


    @Transactional
    public void deleteCache(){
        //删除全部缓存
        List<WxCache> wxCaches = this.wxCacheRepository.findAll();
        for (WxCache wx : wxCaches){
            this.wxCacheRepository.delete(wx.getId());
        }
    }




}
